package com.lanou.moving.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.lanou.moving.R;
import com.lanou.moving.base.BaseApplication;
import com.lanou.moving.utils.MyConfig;
import com.lanou.moving.utils.PreferencesUtils;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.Map;
import java.util.Set;

public class LoginActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, View.OnClickListener {
    private VideoView videoView;
    private ImageView qqloginimg, sinaloginimg;

    //初始化友盟
    UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_login);
        BaseApplication.getInstance().addActivity(this);

        //添加QQ平台
        //参数1为当前activity,参数2为开发者在QQ换联网申请的app id
        //参数3为开发者在qq互联网申请的app key
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(LoginActivity.this, "100424468",
                "c7394704798a158208a74ab60104f0ba");
        qqSsoHandler.addToSocialSDK();
        //添加新浪SSO授权
        mController.getConfig().setSsoHandler(new SinaSsoHandler());


        videoView = (VideoView) findViewById(R.id.videoView);
        qqloginimg = (ImageView) findViewById(R.id.qqloginimg);
        sinaloginimg = (ImageView) findViewById(R.id.sinaloginimg);
        qqloginimg.setOnClickListener(this);
        sinaloginimg.setOnClickListener(this);


        String uri = "android.resource://" + getPackageName() + "/" + R.raw.login_video;
        videoView.setVideoURI(Uri.parse(uri));
        MediaController mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);
        mediaController.setMediaPlayer(videoView);
        videoView.setOnPreparedListener(this);
        videoView.setOnCompletionListener(this);
        videoView.requestFocus();
        videoView.start();
        mediaController.setVisibility(View.GONE);
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
        mp.setLooping(true);
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.start();
        mp.setLooping(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qqloginimg:
                mController.doOauthVerify(LoginActivity.this, SHARE_MEDIA.QQ, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        Toast.makeText(LoginActivity.this, "授权开始", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete(Bundle bundle, SHARE_MEDIA share_media) {
                        Toast.makeText(LoginActivity.this, "授权完成", Toast.LENGTH_SHORT).show();
                        //获取相关授权信息
                        mController.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ,
                                new SocializeListeners.UMDataListener() {
                                    @Override
                                    public void onStart() {
                                        Toast.makeText(LoginActivity.this, "获取平台数据开始...", Toast.LENGTH_SHORT).show();
                                    }

                                    @Override
                                    public void onComplete(int status, Map<String, Object> info) {
                                        Log.i("TESTDATA", "这里status为:" + status);
                                        if (status == 200 && info != null) {
                                            StringBuilder stringBuilder = new StringBuilder();
                                            Set<String> keys = info.keySet();
                                            for (String key : keys) {
                                                stringBuilder.append(key + "=" + info.get(key).toString() + "\r\n");
                                            }
                                            Log.d("TESTDATA", stringBuilder.toString());
                                            String icon = info.get("profile_image_url").toString();

                                            String name = info.get("screen_name").toString();

                                            MyConfig.putSharePre(LoginActivity.this, "platform", "profile_image_url", icon);
                                            MyConfig.putSharePre(LoginActivity.this, "platform", "screen_name", name);

                                            finish();


                                        } else {
                                            Log.d("TESTDATA", "发生错误" + status);
                                        }
                                    }
                                });
                    }

                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA share_media) {
                        Toast.makeText(LoginActivity.this, "授权错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                        Toast.makeText(LoginActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.sinaloginimg:
                mController.doOauthVerify(LoginActivity.this, SHARE_MEDIA.SINA, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA platform) {
                    }

                    @Override
                    public void onComplete(Bundle value, SHARE_MEDIA platform) {
                        if (value != null && !TextUtils.isEmpty(value.getString("uid"))) {
                            Toast.makeText(LoginActivity.this, "授权成功.", Toast.LENGTH_SHORT).show();

                            mController.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.SINA, new SocializeListeners.UMDataListener() {
                                @Override
                                public void onStart() {
                                    Toast.makeText(LoginActivity.this, "获取平台数据开始...", Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onComplete(int status, Map<String, Object> info) {
                                    if (status == 200 && info != null) {
                                        StringBuilder sb = new StringBuilder();
                                        Set<String> keys = info.keySet();
                                        for (String key : keys) {
                                            sb.append(key + "=" + info.get(key).toString() + "\r\n");
                                        }
                                        Log.d("TestData", sb.toString());

                                        String icon = info.get("profile_image_url").toString();

                                        String name = info.get("screen_name").toString();


                                        PreferencesUtils.putString(LoginActivity.this, "profile_image_url", icon);
                                        PreferencesUtils.putString(LoginActivity.this,"screen_name",name);


                                        finish();
                                    } else {
                                        Log.d("TestData", "发生错误：" + status);
                                    }
                                }
                            });


                        } else {
                            Toast.makeText(LoginActivity.this, "授权失败",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                    }

                    @Override
                    public void onStart(SHARE_MEDIA platform) {
                    }
                });
                break;
        }

    }
}
